import React, {Component} from 'react'
import {View, Text, StyleSheet, Modal, ScrollView, TouchableOpacity, Switch} from 'react-native'
import Icon from 'react-native-vector-icons/Ionicons'
import {GetSelectLessonsApi} from '../../config/api'
import StudentsSelector from '../common/students_selector'
import styles from '../../config/styles'

class NewNotification extends Component {
    static navigationOptions = ({ navigation }) => {
        if (navigation.state.params != undefined) {
            const {nextStage} = navigation.state.params
            return {
                headerRight: (  <TouchableOpacity
                                    style={{paddingRight: 5}}
                                    onPress={() => nextStage({key: navigation.state.key})}
                                >
                                    <Text style={[styles.header_font]}>
                                        下一步
                                    </Text>   
                                </TouchableOpacity>)
            }
        }
    }

    constructor (props) {
        super(props)
        this.state = {
            objectForSubmit: {
                title: "",
                description: "",
                students: [],
                attachments: []
            },

            classrooms: [],
          
        }
    }

    componentDidMount () {
        this.getClassrooms()
        const {navigation} = this.props
        const objectForSubmit = this.state.objectForSubmit
        navigation.setParams({
            nextStage: ((params) => {
                if (objectForSubmit.students.length == 0) {
                    Toast.show('请选择学生')
                    return
                }
                navigation.navigate('new_notification_stage2', {objectForSubmit: objectForSubmit, ...params})
            })
        })
    }

    getClassrooms = () => {
        GetSelectLessonsApi().then((data) => {
            if (data.code == 0) {
                const notificlass = data.data.find((x) => x.name == "通知")
                if (notificlass) {
                    this.setState({classrooms: notificlass.classrooms})
                } else {
                    Toast.show('你没有权限进行此操作')
                    this.props.navigation.goBack()
                }
            }
        })
    }


    //将选择学生的函数传给子组件
    studentsSelect = (newSelectedStudents) => {
        this.state.objectForSubmit.students = newSelectedStudents
        this.setState({objectForSubmit: this.state.objectForSubmit})
    }

    render () {
        return (
            <ScrollView style={styles.main_body}>
                {
                    this.state.classrooms.length > 0 ? 
                        ( <StudentsSelector
                            classrooms={this.state.classrooms}
                            studentsSelect={this.studentsSelect}
                            selectedStudents={this.state.objectForSubmit.students}
                        />) : null
                }
               
            </ScrollView>
        )
    }
}

export default NewNotification